package com.lv.common.widget.scale

/**
 * 作者： ygx
 * 创建日期：2023/6/20 9:38 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
import android.animation.Animator
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Animation
import android.view.animation.ScaleAnimation


/**
 * @Description:
 * @Author: JIULANG
 * @Data: 2022/5/12 18:43
 */
object TouchFeedback {
    /**
     * 缩放动画的单方面时长,假如是100,是指放大100,当手指离开缩小又是100.
     */
    private var duration = 333L

    /**
     * 放大或者缩小的值
     */
    private var aFloatBig = 0.9f

    /**
     * 可以自己设定
     *
     * @param duration
     * @param aFloatBig
     */
    fun setDurationAndFloatBig(duration: Long, aFloatBig: Float) {
        this.duration = duration
        this.aFloatBig = aFloatBig
    }

    /**
     * 定义回调的接口
     */
    interface TouchFeedbackListener {
        fun onFeedback(view: View?)
    }

    /**
     * 恢复原形的动画
     * @param v
     * @param b
     * @param touchFeedListener
     */
    fun scaleSmall(v: View?, b: Boolean) {
        val objectAnimator2 = ObjectAnimator.ofFloat(v, "ScaleX", aFloatBig, 1f).setDuration(
            duration
        )
        val objectAnimator3 = ObjectAnimator.ofFloat(v, "ScaleY", aFloatBig, 1f).setDuration(
            duration
        )
        objectAnimator2.start()
        objectAnimator3.start()
        if (b) {
            /**动画执行的监听 */
            objectAnimator2.addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {}

                /**
                 * 动画结束
                 * @param animation
                 */
                override fun onAnimationEnd(animation: Animator) {}
                override fun onAnimationCancel(animation: Animator) {}
                override fun onAnimationRepeat(animation: Animator) {}

            })
        }
    }

    /**
     * 变大的动画
     * @param v
     */
    fun scaleBig(v: View?) {
        val objectAnimator = ObjectAnimator.ofFloat(v, "ScaleX", 1f, aFloatBig).setDuration(
            duration.toLong()
        )
        val objectAnimator1 = ObjectAnimator.ofFloat(v, "ScaleY", 1f, aFloatBig).setDuration(
            duration.toLong()
        )
        objectAnimator.start()
        objectAnimator1.start()
    }

    fun executeAnimation(view: View) {
        val scaleAnimation = ScaleAnimation(
            1.0f,
            0.95f,
            1.0f,
            0.95f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
        scaleAnimation.duration = 100
        scaleAnimation.repeatCount = 1
        scaleAnimation.repeatMode = Animation.REVERSE
        scaleAnimation.interpolator = AccelerateDecelerateInterpolator()
        view.startAnimation(scaleAnimation)
    }

    var mTouchFeedBack: TouchFeedback? = null
    fun getTouchFeedbackInstance(): TouchFeedback {
        if (mTouchFeedBack == null) {
            synchronized(TouchFeedback::class.java) {
                if (mTouchFeedBack == null) {
                    mTouchFeedBack = TouchFeedback
                }
            }
        }
        return mTouchFeedBack!!
    }

}
